<?php
echo "process-start-time:".date("Ymd H:i:s");
$urls = [
     'http://baidu.com',
     'http://sina.com.cn',
     'http://qq.com',
     'http://baidu.com?search=fengqin',
     'http://baidu.com?search=fengqin1',
     'http://baidu.com?search=fengqin2',
     ];
 //传统路数
/* foreach ($urls as $k=>$v)
 {
     $contents[] = file_put_contents($v);
 }*/
 $workers = [];
 //swoole 路数
for ($i=0;$i < count($urls);$i++) {
    //子进程
    $process = new swoole_process(function (swoole_process $worker) use ($i, $urls) {
        //curl
        $content = curlData($urls[$i]);
        //输出 echo $content . PHP_EOL;
        $worker->write($content);
    }, true);
    $pid = $process->start();
    $workers[$pid] = $process;
}


//管道读取数据
foreach ($workers as $worker){
    echo $worker->read();
}

//获取数组内容
/**
 * 模拟请求URL内容 1s
 * @param $url
 * @return string
 */
function curlData($url)
{
    // curl file_get_contents();
    sleep(1);
    return $url ."success".PHP_EOL;
}
echo "process-end-time:".date("Ymd H:i:s");
